﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameSchool.Models;
using GameSchool.Repository;
using M = GameSchool.Models;

namespace GameSchool.Controllers
{
	/// <summary>
	/// PersonController sér um alla virkni tengda skráningu nemenda, kennara 
	/// og umsjónarmanna GameSchool kerfisins. 
	/// </summary> 

	[HandleError]
	[Authorize]
    public class PersonController : Controller
    {
        IGameSchoolRepository m_data = RepositoryMaster.CurrentRepository;


        // GET: /Person/
        public ActionResult Index()
        {
            ViewData["PersonList"] = m_data.Persons.ToList();
            ViewData["CurrentPerson"] = Extra.UserInfo.GetLoggedOnPerson();
            return View(m_data.Persons.ToList());
        }

		[Authorize(Roles = "Administrators")]
		public ActionResult EditPerson(int id)
		{
			var model = m_data.Persons.Find(id);
			return View(model);
		}

		[HttpPost]
		[Authorize (Roles="Administrators")]
		public ActionResult EditPerson(int id, Person pers)
		{
			try
			{
				var item = (from n in m_data.Persons
							where n.PersonId == id
							select n).SingleOrDefault();

				if (item != null)
				{
					UpdateModel(item);
					m_data.SaveChanges();
					return RedirectToAction("Index");
				}
				else
				{
					return RedirectToAction("Index");
				}
			}
			catch (Exception)
			{
				ModelState.AddModelError("", "Ekki tókst að breyta notandanum. Vinsamlegast reyndu aftur.");
			}
			return View(pers);
		}

        public ActionResult StudentCourseGrading()
        {
            Models.Course currentCourse = new Models.Course();
            Models.Person currentStudent = new Models.Person();

            ICollection<Models.OnTheFlyModels.StudentCourseGrading> tmpGradings = new List<Models.OnTheFlyModels.StudentCourseGrading>();
            
            ICollection<Models.Course> myCourses = new List<Models.Course>();
            myCourses.Add(new Models.Course { CourseId = 313, CourseName = "Bíll Andrésar Andar", Description = "Rauður", Ects = 12 });
            myCourses.Add(new Models.Course { CourseId = 911, CourseName = "Hjálp", Description = "Er að deyja" , Ects = 112 });
            myCourses.Add(new Models.Course { CourseId = 505, CourseName = "SOS", Description = "DAMN", Ects = 2 });

            foreach (Models.Course c in myCourses)
            {
                tmpGradings.Add(new Models.OnTheFlyModels.StudentCourseGrading(currentStudent, c));
            }
            return View(tmpGradings);
        }
    }
}
